<?php

namespace App\Http\Controllers\Home;

use App\Http\Controllers\Controller;
use Auth;
use Hash;
use Session;
use Illuminate\Http\Request;
use App\Models\User;
use App\Models\UserInfo;
class IndexController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
//        dd(Session::get('oaHome'));
    }
    public function login(Request $request)
    {
        if($request->isMethod('post'))
        {
            /*不使用laravel自带的登录认证，自己随便搞搞
            1.获取登录信息
            2.查找数据库
            3.错误提示*/
            $username = $request->username;
            $passwd = $request->passwd;
            $userData = User::where('username',$username)->first();
            if(count($userData) == 0)
            {
                //不存在此用户，写入缓存，退加上一页
                $request->session()->flash('errorHint', ['不存在此用户！']);
            }else
            {
                $userData = $userData->toArray();
                if(Hash::check($passwd,$userData['passwd']))
                {
                    //登录成功，保存登录用户的ID，用户名，到session
                    Session::put('oaHome',['id'=>$userData['id'],'username'=>$userData['username']]);
                    return redirect('/index');
                }else
                {
                    $request->session()->flash('errorHint', ['密码错误，请重新输入！']);
                }
            }
        }
        return view('home\login');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function register(Request $request)
    {

        if($request->ajax()){
            $this->validate($request, [
                'username'=>array('regex:/^[\x{4e00}-\x{9fa5}]{2,5}$/u','required','unique:user,username'),
                'passwd'=>array('regex:/^\w{6,12}$/','confirmed','required'),
                'card'=>array('regex:/^(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})([0-9]|X)$/','required'),
                'phone'=>array('regex:/^1[3|4|5|7|8][0-9]\d{8}$/','required'),
            ],[
                'username.regex'=>'用户名不合法，请输入您的中文姓名！',
                'username.required'=>'用户名不能为空！',
                'username.unique'=>'用户名已经存在，请重新输入一个！',
                'passwd.regex'=>'密码格式不符合要求！',
                'passwd.confirmed'=>'两次密码输入不一致！',
                'passwd.required'=>'密码不能为空！',
                'card.regex'=>'身份证号格式不正确！',
                'card.required'=>'身份证号不能为空！',
                'phone.regex'=>'手机号码格式不正确！',
                'phone.required'=>'手机号码不能为空！',
            ]);
            /*如果规则全部符合要求，则创建数据，并插入到数据库
             * 1.创建数据
             * 2.保存数据
             * 3.返回成功消息
            */
            $userModel = new User();
            $userModel->username = $request->username;
            $userModel->passwd = Hash::make($request->passwd);
            $userModel->role = 0;
            $userModel->create_time = time();
            $userModel->save();
            $userInfoModel = new UserInfo();
            $userInfoModel->class_id = 1;
            $userInfoModel->uid = $userModel->id;
            $userInfoModel->sex = $request->sex;
            $userInfoModel->card = $request->card;
            $userInfoModel->address = $request->address;
            $userInfoModel->source = $request->source;
            $userInfoModel->phone = $request->phone;
            $userInfoModel->qq = $request->qq;
            $userInfoModel->email = $request->email;
            $userInfoModel->school = $request->school;
            $userInfoModel->school_address = $request->school_address;
            $userInfoModel->degree = $request->degree;
            $userInfoModel->subject = $request->subject;
            $userInfoModel->basic_computer = $request->basic_computer;
            $userInfoModel->basic_computer_score = $request->basic_computer_score;
            $userInfoModel->target = $request->target;
            $userInfoModel->work_year = $request->work_year;
            $userInfoModel->contacts_name = $request->contacts_name;
            $userInfoModel->contacts_relation = $request->contacts_relation;
            $userInfoModel->contacts_phone = $request->contacts_phone;
            if($userInfoModel->save())
            {
                $ajaxReturn = array('status'=>0,'message'=>'注册成功！','url'=>url('index'));
            }else
            {
                $ajaxReturn = array('status'=>1,'message'=>'注册失败，请联系管理员！');
            }
            return response()->json($ajaxReturn);
        }
        return view('home\register');
    }
    /*
     * 验证单个字段
     * */
//    public function validateOneField(Request $request){
//        $this->validate($request, [
//            'title' => 'required|unique:posts|max:255',
//            'body' => 'required',
//        ]);
//
//        // 验证通过，存储到数据库...
//    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
